<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/profile.php';
	
	//Get user
	$id = 0;
	if (isset($_GET['id'])) $id = $_GET['id'];

	$user	= NULL;
	$result = mysql_query("SELECT * FROM users WHERE id=$id") or die(mysql_error());
	if (mysql_num_rows($result) > 0) $user = mysql_fetch_array($result);
	
	//Get pages
	$page = 1;
	if (isset($_GET['page'])) $page = $_GET['page'];	
	
	//Get photos
	$photos = array();
	$query = "SELECT photos.id AS id, photos.photo AS photo, photos.title AS title, photos.time_uploaded AS time FROM photos,tags WHERE photos.id=tags.photo_id AND tags.user_id=". $user['id'] ." ORDER BY time DESC";
	$result = mysql_query($query) or die(mysql_error());
			
	$index = 0;
	$index_max = mysql_num_rows($result);

	$pages = ceil($index_max/$pictures_in_page);
	$index = $pictures_in_page * ($page - 1);
	$index_max = ($index_max > $index + $pictures_in_page) ? $index + $pictures_in_page : $index_max;
	
	$photos = array();
	$query = "SELECT photos.id AS id, photos.photo AS photo, photos.title AS title, photos.time_uploaded AS time FROM photos,tags WHERE photos.id=tags.photo_id AND tags.user_id=". $user['id'] ." ORDER BY time DESC LIMIT $index, $index_max";
	$result = mysql_query($query) or die(mysql_error());
	
	while($index<$index_max) {
		$row = mysql_fetch_array($result);
		$photos[] = $row;
		$index++;
	}
?>

		<h3 id="content-head"> <?php echo $user['username']?>'s Gallery</h3>
		
		<!-- Gallery Holder -->
		<div id="gallery-holder">
			<?php 
				foreach ($photos as $photo) {
					echo ' <span class="photo-thumbnail">
						<a href="/deviantpic/picture/view.php?id='.$photo['id'].'">
							<img src="' . $photo['photo'] . '" alt= ' . $photo['title'] . ' width="200" height="200"/>
						</a>
					</span>
					';
				}
			?>
		</div>
		
		<!-- Page Navigation -->
		<?php 
		$page_now=1;
	    while($pages) {
	        if ($page_now==$page) {
	            echo "<a href=\"javascript:goto_gallery_page(".$page_now.",".$id.");\"><b>[".$page_now."]</b></a>&nbsp;&nbsp;&nbsp;\n";
	        } else {
	            echo "<a href=\"javascript:goto_gallery_page(".$page_now.",".$id.");\">".$page_now."</a>&nbsp;&nbsp;&nbsp;\n";
	        }
	        $pages--;
	        $page_now++;
	    }
		?>
		